<?php
/**
 * 基础操作——查
 */

class ViewCTR extends Controller
{
    private $config;
    function init()
    {
        $ConfigModel = new Model('config');
        $config = $ConfigModel->fetch();
        $this->config = $config;
        $this->assign('config', $config);
    }

    function announcement()
    {
        $id = $_GET['id'] + 0 ? $_GET['id'] + 0 : '1';
        $AnnouncementModel = new Model('announcement');
        $announcement = $AnnouncementModel->fetch('*', array('id' => $id));
        if (!empty($announcement)) {
            $num = $AnnouncementModel->addByAttr('visits', array('id' => $id));
            $this->assign('readcount', $num);
            $this->assign('title', $announcement['title'] . ' - ' . $this->config['name']);
            $this->assign('announcement', $announcement);
            $this->display();
        } else {
            $this->error('非法访问', $this->urlCreate('index', 'index'));
        }
    }

    function topic()
    {
        if (isset($_GET['id']) && !empty($_GET['id'])) {
            $id = $_GET['id'] + 0;
            $TopicModel = new Model('topic');
            $topic = $TopicModel->fetch('*', array('id' => $id));
            if ($topic) {
                $SectionModel = new Model('section');
                $section = $SectionModel->fetch('*', array('id' => $topic['sid']));
                $ForumModel = new Model('forum');
                $forum = $ForumModel->fetch('*', array('id' => $section['fid']));
                $ReplyModel = new Model('reply');
                $reply = $ReplyModel->fetchAll('*', array('tid' => $id));
                $UserModel = new Model('user');
                $user = $UserModel->fetch('*', array('id' => $topic['uid']));
                $UploadModel = new Model('upload');
                $uploads = $UploadModel->fetchAll('*', array('type' => 'topic', 'tid' => $id));
                $num = $TopicModel->addByAttr('visits', array('id' => $id));
                $this->assign('readcount', $num);
                $this->assign('uploads', $uploads);
                $this->assign('login_user', Session::get('user'));
                $this->assign('section', $section);
                $this->assign('forum', $forum);
                $this->assign('user', $user);
                $this->assign('title', $topic['title'] . ' - ' . $this->config['name']);
                $this->assign('topic', $topic);
                $this->assign('replys', $reply);
                $this->display();
            } else {
                $this->error('该帖不存在', $this->urlCreate('index', 'index'));
            }
        } else {
            $this->error('非法操作', $this->urlCreate('index', 'index'));
        }
    }

    protected function getUserByID($id)
    {
        $UserModel = new Model('user');
        $user = $UserModel->fetch('*', array('id' => $id));
        if ($user) {
            return $user['user'];
        } else {
            return '黑户';
        }
    }
}

?>